/*
 * @Author: i@rysa.cn
 * @Date: 2022-05-14 16:07:17
 * @LastEditTime: 2022-05-14 16:07:28
 * @LastEditors: i@rysa.cn
 * @Description:获取手机号
 * @FilePath: \go-utils\wechat\mini\sns\phone.go
 */
package sns

import (
	"encoding/json"
	"fmt"

	"gitee.com/rysa/go-utils/request"
	accesstoken "gitee.com/rysa/go-utils/wechat/mini/access_token"
	"gitee.com/rysa/go-utils/wechat/mini/params"
)

func (s *sns) GetUserPhone(code string) params.PhoneResult {
	token, err := accesstoken.NewAccessToken(s.MiniConfig).GetAccessToken()
	if err != nil {
		return params.PhoneResult{
			Errcode: -1,
			Errmsg:  err.Error(),
		}
	}
	url := fmt.Sprintf("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s", token)
	req := request.NewRequest()
	req.SetBody(map[string]string{
		"code": code,
	})
	res, err := req.Post(url)
	fmt.Println(res.String())
	if err != nil {
		return params.PhoneResult{
			Errcode: -1,
			Errmsg:  err.Error(),
		}
	}
	var rs params.PhoneResult
	json.Unmarshal(res.Body(), &rs)
	return rs
}
